<template>
  <div class="full-card" @click="todetail(i)" ref="item">
    <div class="head">
      <span class="left"
        ><!-- @click.stop="toUserPage" -->
        <van-image round class="avatar" :src="i.avatar_thumb" />
        <span class="u-name">{{ i.user_nicename }}</span>
      </span>
      <!-- <span class="right"><van-icon name="arrow-down"/></span> -->
    </div>
    <div v-if="i.type == 3" :class="i.type == 3 ? 'title-txt' : 'title'">
      <div class="left-t">短文</div>
      {{ i.title }}
    </div>
    <div v-else style="margin:0 10px;margin-bottom:10px;">
      {{ i.title }}
    </div>
    <div class="m-pics" v-if="i.type == 4">
      <van-swipe>
        <van-swipe-item v-for="(img, index) in i.images.split(',')" :key="img">
          <div
            style="text-align:center;height:100%;width:100%;overflow:hidden;"
          >
            <img
              :src="$host + img"
              style="height:100%;"
              @click.stop="preview(index)"
            />
          </div>
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="text-content" v-if="i.type == 3">
      {{ i.content }}
    </div>
    <div class="video-content" v-if="i.type == 5">
      <video
        style="width:100%;"
        :poster="i.video_thumb ? $host + i.video_thumb : ''"
        :controls="played"
        ref="m-video"
        @click.stop="play($event)"
      />
    </div>
    <div v-if="i.tag">
      <ti
        v-for="t in i.tag.split(',')"
        :key="t"
        @click.native.stop="searchTag(t)"
        :text="t"
      />
    </div>
    <div class="share-actions">
      <div class="left" @click.stop="">
        <van-icon
          name="share"
          v-clipboard:copy="share"
          v-clipboard:success="onCopy"
          v-clipboard:error="onError"
          class="icon"
        />
        <span
          class="num"
          v-clipboard:copy="share"
          v-clipboard:success="onCopy"
          v-clipboard:error="onError"
          >{{ i.view }}</span
        >
      </div>
      <div class="middle">
        <van-icon name="chat-o" class="icon" />
        <!-- <van-icon name="smile-comment-o" class="icon" /> -->
        <span class="num">{{ i.comment }}</span>
      </div>
      <div class="right">
        <van-icon name="star-o" class="icon" @click.stop="postCollect" />
        <span class="num" @click.stop="postCollect">{{ i.collect }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { ImagePreview } from "vant";
import ti from "@/components/TagIcon";
export default {
  name: "topic-item",
  props: ["i", "checkPlay"],
  components: { ti },
  data() {
    return {
      share: window.sessionStorage.getItem("share"),
      played: false,
      images: []
    };
  },
  methods: {
    toUserPage() {
      window.location.href = "http://tomcat.apache.org/";
    },
    preview(index) {
      ImagePreview({
        images: this.images,
        startPosition: index,
        onClose() {}
      });
    },
    searchTag(tag) {
      this.$router.push({
        name: "search",
        query: {
          tag: tag
        }
      });
    },
    checkFreeToday(e) {
      let k = "f_time_" + new Date().toLocaleDateString();
      k = k.replace("/", "").replace("/", "");

      let s = localStorage.getItem("videos");
      if (s) {
        s = JSON.parse(s);
      } else {
        s = [];
      }
      if (s.filter(v => v == this.i.id).length > 0) {
        this.doPlay(e);
        return true;
      }
      //免费观影次数
      const freeTimes = 3;
      let leftTimes = localStorage.getItem(k);
      if (leftTimes == 0) {
        return false;
      }
      if (null == leftTimes) {
        leftTimes = freeTimes;
      }
      if (leftTimes > 0) {
        leftTimes--;
        let _this = this;
        this.$dialog
          .alert({
            title: "温馨提示",
            message: "今日免费观影次数剩余" + leftTimes + "次！"
          })
          .then(() => {
            s.push(_this.i.id);
            localStorage.setItem("videos", JSON.stringify(s));
            _this.doPlay(e);
          });
      }
      localStorage.setItem(k, leftTimes);
      return true;
    },
    needVip(e) {
      let isPlay = this.checkFreeToday(e);
      if (!isPlay) {
        this.$dialog
          .alert({
            title: "温馨提示",
            message: "无播放次数，请升级会员！"
          })
          .then(() => {});
      }
    },
    play(e) {
      if (this.checkPlay) {
        if (this.played) {
          this.doPlay(e);
        } else {
          this.$api.PostViewVideo(this.i.id).then(r => {
            if (r.data.code == 0) {
              let allSeeNum = r.data.info.allSeeNum;
              let leftSeeNum = r.data.info.leftSeeNum;
              window.sessionStorage.setItem("allSeeNum", allSeeNum);
              window.sessionStorage.setItem("leftSeeNum", leftSeeNum);
              if (leftSeeNum == -1) {
                this.needVip(e);
                return;
              } else {
                this.doPlay(e);
                return;
              }
            } else {
              this.needVip(e);
              return;
            }
          });
        }
      } else {
        this.doPlay(e);
      }
    },
    doPlay(e) {
      e.target.src = this.$host + this.i.video;
      e.target.play();
      this.played = true;
      window.addEventListener("scroll", this.scrollHandle, true);
    },
    scrollHandle() {
      const offset = this.$el.getBoundingClientRect(); // vue中，使用this.$el获取当前组件的根元素
      const offsetTop = offset.top;
      const offsetBottom = offset.bottom;
      const offsetHeight = offset.height;
      // 进入可视区域
      if (offsetTop <= window.innerHeight && offsetBottom >= 0) {
        console.log("进入可视区域");
      } else {
        console.log("移出可视区域");
        window.removeEventListener("scroll", this.scrollHandle, true);
        this.$refs["m-video"].pause();
      }
    },
    onCopy(e) {
      this.$toast("已复制成功，快去分享吧~");
    },
    onError(e) {
      this.$toast("复制失败了~");
    },
    todetail() {
      this.$router.push({ name: "detail", params: this.i });
    },
    postCollect() {
      this.$api.PostCollect(this.i.id).then(r => {
        // console.log(r);
        if (r.data.code == 0) {
          if (r.data.info.id) {
            this.$toast("收藏成功~");
            this.i.collect++;
          } else {
            this.$toast("已取消收藏~");
            this.i.collect--;
          }
        }
      });
    }
  },
  created() {
    if (this.i.avatar_thumb && this.i.avatar_thumb.indexOf("http") == -1) {
      this.i.avatar_thumb = this.$host + this.i.avatar_thumb;
    }
    if (this.i.type == 4) {
      this.images = this.i.images.split(",");
      let _this = this;
      for (var i = 0; i < this.images.length; i++) {
        this.images[i] = this.$host + this.images[i];
      }
    }
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollHandle, true);
  }
};
</script>

<style lang="less">
.my-preview {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
</style>
<style lang="less" scoped>
.full-card {
  border-bottom: 1px solid #eee;
  box-shadow: 0 0px 1px 0px #eee;
  font-size: 20px;
  .head {
    padding: 10px;
    .left {
      width: 330px;
      display: inline-block;
      .avatar {
        width: 30px;
        height: 30px;
        vertical-align: middle;
      }
      .u-name {
        margin-left: 10px;
        font-size: 14px;
      }
    }
    .right {
      color: #97969c;
    }
  }
  .title {
    padding: 0 10px;
    line-height: 40px;
    font-weight: 100;
  }
  .title-txt {
    padding: 5px 20px;
    line-height: 40px;
    font-weight: 100;
    background: #f6f5fa;
    margin: 0 10px;
    font-size: 17px;
    border-radius: 5px;
    position: relative;
    .left-t {
      font-size: 10px;
      position: absolute;
      top: 0;
      left: 0;
      height: 16px;
      line-height: 16px;
      background: #fa98a4;
      color: #fff;
      padding: 1px 5px;
      border-radius: 5px 0 12px 0;
    }
  }
  .share-actions {
    padding: 10px;
    display: flex;
    .left,
    .middle,
    .right {
      flex-basis: 33%;
    }
    .middle {
      text-align: center;
    }
    .right {
      text-align: right;
    }
    .icon {
      font-size: 20px;
      vertical-align: middle;
    }
    .num {
      font-size: 18px;
      vertical-align: middle;
    }
  }
}
.text-content {
  padding: 0 13px;
  margin: 6px 0;
  font-size: 15px;
  max-height: 60px;
  line-height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: justify;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  letter-spacing: 1px;
}
.video-content {
  background: #000;
  video {
    max-height: 370px;
  }
}
.m-pics {
  .van-swipe {
    height: 400px;
    background-color: #333;
  }
  .custom-indicator {
    position: absolute;
    right: 5px;
    bottom: 5px;
    padding: 2px 5px;
    color: #fff;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.1);
  }
}
</style>
